#include"yuechuan.h"
#include<unistd.h>

yuechuan::Logger::ptr g_logger = YUECHUAN_LOG_ROOT();

int count = 0;
yuechuan::RWMutex s_mutex;
yuechuan::Mutex mutex;
void func1()
{
    YUECHUAN_LOG_INFO(g_logger) << " name: "<< yuechuan::Thread::GetName()
                                <<" this.name: "<< yuechuan::Thread::GetThis()->getName()
                                <<" id: " << yuechuan::GetThreadId()
                                <<" this.id: "<<yuechuan::Thread::GetThis()->getId();
    for(int i =0;i<100000;++i)
    {
        // yuechuan::RWMutex::Writelock lock(s_mutex);
//        yuechuan::Mutex::MutexLock lock(mutex);
        yuechuan::RWMutex::ReadLock lock(s_mutex);
        count ++;
    }
}
void func2()
{
    while(true)
    {
        YUECHUAN_LOG_INFO(g_logger) << "------";
    }
}
void func3()
{
    while(true)
    {
        YUECHUAN_LOG_INFO(g_logger) << "======";
    }
}
int main(int argc, char** argv)
{
    std::cout << "hello world" << std::endl;
    std::vector<yuechuan::Thread::ptr> thread_pool;
    YUECHUAN_LOG_INFO(g_logger) << "thread test begin";
    YAML::Node root = YAML::LoadFile("/home/yuechuan/Project/CProject/yuechuan/bin/conf/log2.yaml");
    yuechuan::Config::LoadFromYaml(root);

    for (int i = 0; i < 5; ++i)
    {
        yuechuan::Thread::ptr thread(new yuechuan::Thread(&func2, "name_"+ std::to_string(i * 2)));
        yuechuan::Thread::ptr thread2(new yuechuan::Thread(&func3, "name_"+ std::to_string(i * 2 + 1)));
        thread_pool.push_back(thread);
        thread_pool.push_back(thread2);
//        yuechuan::Thread::ptr thread(new yuechuan::Thread(&func1, "name_" + std::to_string(i)));
//        thread_pool.push_back(thread);
    }
    for (size_t i = 0; i < thread_pool.size(); ++i)
    {
        thread_pool[i]->join();
    }
    YUECHUAN_LOG_INFO(g_logger) << "thread test end";
    YUECHUAN_LOG_INFO(g_logger) << " count =" << count;
    return 0;
}